<?php

/**
 * This is the model class for table "{{menu}}".
 *
 * The followings are the available columns in table '{{menu}}':
 * @property integer $id
 * @property string $name
 * @property string $position
 * @property string $icon
 * @property integer $rank
 * @property string $language_id
 */
class Menu extends MyARContent
{

    const POS_NAVBAR = 1;
    const POS_FOOTER = 2;
    const POS_BOTTOM = 3;
    const POS_MAP = 'sitemap';
    const POS_ABOUTUS = 'aboutus';
    const POS_QUESTION = 'question';

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Menu the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{menu}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, position', 'required'),
            array('rank', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 64),
            array('position', 'length', 'max' => 255),
            array('icon', 'length', 'max' => 30),
            array('language_id, icon', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, position, icon, rank, language_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        $relations = parent::relations();
        $relations['items'] = array(
            self::HAS_MANY, 'MenuItem', 'menu_id', 'condition' => 'parent_id=0',
            'order' => 'rank desc'
        );

        return $relations;
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        $array = array(
            'id' => 'ID',
            'name' => 'Name',
            'position' => 'Position',
            'icon' => 'Icon',
            'rank' => 'Rank',
            'language_id' => 'Language',
        );
        foreach ($array as $key => $val)
        {
            $array[$key] = Yii::t('menu', $val);
        }

        return $array;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('position', $this->position, true);
        $criteria->compare('icon', $this->icon, true);
        $criteria->compare('rank', $this->rank);
        $criteria->compare('language_id', $this->language_id, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    function getObjectName()
    {
        return $this->name;
    }

    static function getArrayPosition()
    {
        return array(
            self::POS_NAVBAR => Yii::t('menu', 'Navbar'),
            self::POS_FOOTER => Yii::t('menu', 'Footer'),
            self::POS_BOTTOM => Yii::t('menu', 'Bottom'),
            self::POS_MAP => Yii::t('menu', 'Site Map'),
            self::POS_ABOUTUS => Yii::t('menu', 'About us'),
            self::POS_QUESTION => Yii::t('menu', 'Question'),
        );
    }

    static function getItems($position = NULL)
    {
        $menuItems = array();
        $cri = new CDbCriteria;
        $cri->condition = 'position=:p';
        $cri->order = 'rank ASC';
        $cri->params = array(':p' => $position);
        $menu = self::model()->find($cri);
        if ($menu == NULL)
            return $menuItems;
        foreach ($menu->items as $key => $item)
        {
            $childItems = array();
            /*
             * Danh mục của bài viết
             */
            if ($item->type == MenuItem::TYPE_CATEGORY)
            {
                $category = Category::model()->findByPk($item->value);
                $link = $category->publicUrl;

                foreach ($item->children as $menu_item)
                {
                    if ($menu_item->type == MenuItem::TYPE_PAGE)
                    {
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => array($menu_item->value),
                        );
                    }
                    elseif ($menu_item->type == MenuItem::TYPE_CATEGORY)
                    {
                        $category_child = Category::model()->findByPk($menu_item->value);
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => @$category_child->publicUrl,
                        );
                    }
                    elseif ($menu_item->type == MenuItem::TYPE_CONTROLLER)
                    {
                        $link = Yii::app()->createUrl("$menu_item->value");
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => $link,
                        );
                    }
                    elseif ($menu_item->type == MenuItem::TYPE_LINK)
                    {
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => $menu_item->value,
                        );
                    }
                    if ($menu_item->type == MenuItem::TYPE_POST)
                    {
                        $post = Post::model()->findByPk($menu_item->value);
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => $post->publicUrl,
                        );
                    }
                }

                $menuItems[] = array(
                    'items' => $childItems,
                    'label' => $item->name,
                    'url' => $link,
                    'type' => $item->type,
                    'value' => $item->value,
                    'itemOptions' => array('rel' => "nofollow"),
                    'submenuOptions' => array('class' => 'has-sub'),
                    'active' => (isset($_GET['url']) && $category->url == $_GET['url'])
                );
            }
            /*
             * Nếu là page
             */
            elseif ($item->type == MenuItem::TYPE_PAGE)
            {
                foreach ($item->children as $menu_item)
                {
                    if ($menu_item->type == MenuItem::TYPE_PAGE)
                    {
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => array($menu_item->value),
                        );
                    }
                    elseif ($menu_item->type == MenuItem::TYPE_CATEGORY)
                    {
                        $category_child = Category::model()->findByPk($menu_item->value);
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => @$category_child->publicUrl,
                        );
                    }
                }

                $menuItems[] = array(
                    'items' => $childItems,
                    'label' => $item->name,
                    'url' => array($item->value),
                    'itemOptions' => array('rel' => "nofollow"),
                    'submenuOptions' => array('class' => 'has-sub'),
                );
            }
            /*
             * Nếu là post
             */
            if ($item->type == MenuItem::TYPE_POST)
            {
                $post = Post::model()->find(array(
                    'condition' => 'id = :c',
                    'params' => array(
                        ':c' => $item->value,
                    ),
                ));
                if ($post)
                {
                    $link = $post->publicUrl;
                }
                if ($item->children)
                {
                    foreach ($item->children as $menu_item)
                    {
                        if ($menu_item->type == MenuItem::TYPE_PAGE)
                        {
                            $childItems[] = array('label' => $menu_item->name,
                                'url' => array($menu_item->value),
                            );
                        }
                        elseif ($menu_item->type == MenuItem::TYPE_CATEGORY)
                        {
                            $category_child = Category::model()->findByPk($menu_item->value);
                            $childItems[] = array('label' => $menu_item->name,
                                'url' => @$category_child->publicUrl,
                            );
                        }
                        elseif ($menu_item->type == MenuItem::TYPE_LINK)
                        {
                            $childItems[] = array('label' => $menu_item->name,
                                'url' => $menu_item->value,
                            );
                        }
                        elseif ($menu_item->type == MenuItem::TYPE_CONTROLLER)
                        {
                            $link = Yii::app()->createUrl("$menu_item->value");
                            $childItems[] = array('label' => $menu_item->name,
                                'url' => $link,
                            );
                        }
                        if ($menu_item->type == MenuItem::TYPE_POST)
                        {
                            $post = Post::model()->findByPk($menu_item->value);
                            $childItems[] = array('label' => $menu_item->name,
                                'url' => $post->publicUrl,
                            );
                        }
                    }
                }

                $menuItems[] = array(
                    'label' => $item->name,
                    'items' => $childItems,
                    'url' => $link,
                    'itemOptions' => array('rel' => "nofollow"),
                    'submenuOptions' => array('class' => 'has-sub'),
                    'active' => (isset($_GET['url']) && $post->url == $_GET['url'])
                );
            }

            /**
             * 
             */
            if ($item->type == MenuItem::TYPE_CONTROLLER)
            {
                $link = Yii::app()->createUrl("$item->value");
                /*
                 * Lấy danh mục con
                 */

                foreach ($item->children as $menu_item)
                {
                    if ($menu_item->type == MenuItem::TYPE_PAGE)
                    {
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => array($menu_item->value),
                        );
                    }
                    elseif ($menu_item->type == MenuItem::TYPE_CATEGORY)
                    {
                        $category_child = Category::model()->findByPk($menu_item->value);
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => @$category_child->publicUrl,
                        );
                    }
                    elseif ($menu_item->type == MenuItem::TYPE_LINK)
                    {
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => $menu_item->value,
                        );
                    }
                    elseif ($menu_item->type == MenuItem::TYPE_CONTROLLER)
                    {
                        $link = Yii::app()->createUrl("$menu_item->value");
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => $link,
                        );
                    }
                    if ($menu_item->type == MenuItem::TYPE_POST)
                    {
                        $post = Post::model()->findByPk($menu_item->value);
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => $post->publicUrl,
                        );
                    }
                }
                $menuItems[] = array(
                    'label' => $item->name,
                    'url' => $link,
                    'url' => $link,
                    'type' => $item->type,
                    'value' => $item->value,
                    'itemOptions' => array('rel' => "nofollow"),
                    'items' => $childItems,
                    'submenuOptions' => array('class' => 'has-sub'),
                    'active' => !isset($_GET['url']) && Yii::app()->controller->id . '/' . Yii::app()->controller->action->id == $item->value
                );
            }

            /*
             * Nếu là link bên ngoài
             */
            if ($item->type == MenuItem::TYPE_LINK)
            {
                foreach ($item->children as $menu_item)
                {
                    if ($menu_item->type == MenuItem::TYPE_PAGE)
                    {
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => array($menu_item->value),
                        );
                    }
                    elseif ($menu_item->type == MenuItem::TYPE_CATEGORY)
                    {
                        $category_child = Category::model()->findByPk($menu_item->value);
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => @$category_child->publicUrl,
                        );
                    }
                    elseif ($menu_item->type == MenuItem::TYPE_LINK)
                    {
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => $menu_item->value,
                        );
                    }
                    elseif ($menu_item->type == MenuItem::TYPE_CONTROLLER)
                    {
                        $link = Yii::app()->createUrl("$menu_item->value");
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => $link,
                        );
                    }
                    if ($menu_item->type == MenuItem::TYPE_POST)
                    {
                        $post = Post::model()->findByPk($menu_item->value);
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => $post->publicUrl,
                        );
                    }
                }

                $menuItems[] = array(
                    'items' => $childItems,
                    'label' => $item->name,
                    'url' => $item->value,
                    'submenuOptions' => array('class' => 'has-sub'),
                    'itemOptions' => array('rel' => "nofollow"),
                );
            }
            if ($item->type == MenuItem::TYPE_GENERAL)
            {
                foreach ($item->children as $menu_item)
                {
                    if ($menu_item->type == MenuItem::TYPE_PAGE)
                    {
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => array($menu_item->value),
                        );
                    }
                    elseif ($menu_item->type == MenuItem::TYPE_CATEGORY)
                    {
                        $category_child = Category::model()->findByPk($menu_item->value);
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => @$category_child->publicUrl,
                        );
                    }
                    elseif ($menu_item->type == MenuItem::TYPE_LINK)
                    {
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => $menu_item->value,
                        );
                    }
                    elseif ($menu_item->type == MenuItem::TYPE_CONTROLLER)
                    {
                        $link = Yii::app()->createUrl("$menu_item->value");
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => $link,
                        );
                    }
                    if ($menu_item->type == MenuItem::TYPE_POST)
                    {
                        $post = Post::model()->findByPk($menu_item->value);
                        $childItems[] = array('label' => $menu_item->name,
                            'url' => $post->publicUrl,
                        );
                    }
                }

                $menuItems[] = array(
                    'items' => $childItems,
                    'label' => $item->name,
                    'url' => Yii::app()->createUrl('general/index', array('url' => $item->value)),
                    'submenuOptions' => array('class' => 'has-sub'),
                    'itemOptions' => array('rel' => "nofollow"),
                );
            }
        }
        return $menuItems;
    }

    public static function getPublicArray($blacklist = array())
    {
        $criteria = new CDbCriteria;
        $criteria->condition = "language_id=:l";
        $criteria->params = array(
            ':l' => Yii::app()->controller->lang_id,
        );

        $allPublic = self::model()->findAll($criteria);
        $array = array(
            0 => Yii::t('menu', 'Root Menu')
        );
        if ($allPublic != NULL)
        {
            foreach ($allPublic->items as $item)
            {
                foreach ($item->children as $child_item)
                {
                    if (in_array($child_item->id, $blacklist))
                        continue;
                    $array[$child_item->id] = $child_item->name;
                }
            }
        }
        return $array;
    }

    public static function getPageLink()
    {
        $array = array(
            'site/index' => 'Trang chủ',
            'site/about' => 'Trang giới thiệu',
            'service/index' => 'Trang dịch vụ',
            'download/index' => 'Trang Download',
            'post/index' => 'Trang tin tức',
            'course/index' => 'Trang khóa học',
            'site/contact' => 'Trang liên hệ',
        );
        return $array;
    }

    public static function getCateMenu()
    {
        $cri = new CDbCriteria;
        $cri->condition = "parent_id=:parent";
        $cri->params = array(
            ':parent' => 0,
        );
        $cates = MenuItem::model()->findAll($cri);
        foreach ($cates as $cate)
        {
            self::getSubCateMenu($cate);
        }
    }

    static function getSubCateMenu($cate)
    {
        if (!empty($cate->children))
        {
            echo "<li class='has-sub'>$cate->name</a>\n";
            echo "<ul>\n";
            foreach ($cate->children as $subcate)
            {
                self::getSubCateMenu($subcate);
            }
            echo "</ul>\n";
            echo "</li>\n";
        }
        else
        {
            echo "<li><a href='$cate->publicUrl'>$cate->name</a></li>\n";
        }
    }

}
